/*----------------------------------------------------------------------------
 * Name:    UTF8.c
 * Version: V1.00
 *----------------------------------------------------------------------------
 * provide function to convert an UTF-8 string to ASCII string
 * Copyright (c) 2011 Golden Key Company. All rights reserved.
 *----------------------------------------------------------------------------*/

#include "UTF8.h"

/*
 * ***********************************************
 * check if an element belong to 'A' group
 *
 * return 1 if Upcase
 * 		  2 if Lowcase
 * 		  0 if wrong
 * ***********************************************
 */
int isA(long element) {
	long Agroup[] = { 193, 192, 195, 7842, 7840, 194, 7844, 7846, 7850, 7848,
			7852, 258, 7854, 7856, 7860, 7858, 7862, 0 };
	long agroup[] = { 225, 224, 227, 7843, 7841, 226, 7845, 7847, 7851, 7849,
			7853, 259, 7855, 7857, 7861, 7859, 7863, 0 };
	int pos = 0;
	while (Agroup[pos] != 0) {
		if (element == Agroup[pos]) {
			return 1;
		} else {
			pos++;
		}
	}
	pos = 0;
	while (agroup[pos] != 0) {
		if (element == agroup[pos]) {
			return 2;
		} else {
			pos++;
		}
	}
	return 0;
}

/*
 * ***********************************************
 * check if an element belong to 'E' group
 *
 * return 1 if right
 * 		  0 if wrong
 * ***********************************************
 */
int isE(long element) {
	long Egroup[] = { 201, 200, 7868, 7866, 7864, 202, 7870, 7872, 7876, 7874,
			7878, 0 };
	long egroup[] = { 233, 232, 7869, 7867, 7865, 234, 7871, 7873, 7877, 7875,
			7879, 0 };
	int pos = 0;
	while (Egroup[pos] != 0) {
		if (element == Egroup[pos]) {
			return 1;
		} else {
			pos++;
		}
	}
	pos = 0;
	while (egroup[pos] != 0) {
		if (element == egroup[pos]) {
			return 2;
		} else {
			pos++;
		}
	}
	return 0;
}

/*
 * ***********************************************
 * check if an element belong to 'I' group
 *
 * return 1 if right
 * 		  0 if wrong
 * ***********************************************
 */
int isI(long element) {
	long Igroup[] = { 205, 204, 296, 7880, 7882, 0 };
	long igroup[] = { 237, 236, 297, 7881, 7883, 0 };
	int pos = 0;
	while (Igroup[pos] != 0) {
		if (element == Igroup[pos]) {
			return 1;
		} else {
			pos++;
		}
	}
	pos = 0;
	while (igroup[pos] != 0) {
		if (element == igroup[pos]) {
			return 2;
		} else {
			pos++;
		}
	}
	return 0;
}

/*
 * ***********************************************
 * check if an element belong to 'O' group
 *
 * return 1 if right
 * 		  0 if wrong
 * ***********************************************
 */
int isO(long element) {
	long Ogroup[] = { 211, 210, 213, 7886, 7884, 212, 7888, 7890, 7894, 7892,
			7896, 416, 7898, 7900, 7904, 7902, 7906, 0 };
	long ogroup[] = { 243, 242, 245, 7887, 7885, 244, 7889, 7891, 7895, 7893,
			7897, 417, 7899, 7901, 7905, 7903, 7907, 0 };
	int pos = 0;
	while (Ogroup[pos] != 0) {
		if (element == Ogroup[pos]) {
			return 1;
		} else {
			pos++;
		}
	}
	pos = 0;
	while (ogroup[pos] != 0) {
		if (element == ogroup[pos]) {
			return 2;
		} else {
			pos++;
		}
	}
	return 0;
}

/*
 * ***********************************************
 * check if an element belong to 'U' group
 *
 * return 1 if right
 * 		  0 if wrong
 * ***********************************************
 */
int isU(long element) {
	long Ugroup[] = { 218, 217, 360, 7910, 7908, 431, 7912, 7914, 7918, 7916,
			7920, 0 };
	long ugroup[] = { 250, 249, 361, 7911, 7909, 432, 7913, 7915, 7919, 7917,
			7921, 0 };
	int pos = 0;
	while (Ugroup[pos] != 0) {
		if (element == Ugroup[pos]) {
			return 1;
		} else {
			pos++;
		}
	}
	pos = 0;
	while (ugroup[pos] != 0) {
		if (element == ugroup[pos]) {
			return 2;
		} else {
			pos++;
		}
	}
	return 0;
}

/*
 * ***********************************************
 * check if an element belong to 'Y' group
 *
 * return 1 if right
 * 		  0 if wrong
 * ***********************************************
 */
int isY(long element) {
	long Ygroup[] = { 221, 7922, 7928, 7926, 7924, 0 };
	long ygroup[] = { 253, 7923, 7929, 7927, 7925, 0 };
	int pos = 0;
	while (Ygroup[pos] != 0) {
		if (element == Ygroup[pos]) {
			return 1;
		} else {
			pos++;
		}
	}
	pos = 0;
	while (ygroup[pos] != 0) {
		if (element == ygroup[pos]) {
			return 2;
		} else {
			pos++;
		}
	}
	return 0;
}

/*
 * ***********************************************
 * check if an element belong to 'D' group
 *
 * return 1 if right
 * 		  0 if wrong
 * ***********************************************
 */
int isD(long element) {
	long Dgroup[] = { 272, 0 };
	long dgroup[] = { 273, 0 };
	int pos = 0;
	while (Dgroup[pos] != 0) {
		if (element == Dgroup[pos]) {
			return 1;
		} else {
			pos++;
		}
	}
	pos = 0;
	while (dgroup[pos] != 0) {
		if (element == dgroup[pos]) {
			return 2;
		} else {
			pos++;
		}
	}
	return 0;
}

/*
 * ****************************************************************
 * Function convert from an UTF-8 string to ASCII string
 *
 * UTF-8 string is strIN
 * ASCII string is strOUT
 * ****************************************************************
 */
void UTF8toASCII(const char *strIN, char *strOUT) {
	int posIN;
	int posOUT;
	long temp;

	posIN = 0;
	posOUT = 0;

	while (strIN[posIN] != '\0') {
		temp = 0;
		if (strIN[posIN] >= 224) {
			temp = (strIN[posIN] - 224) << 12;
			posIN++;
			temp = temp + ((strIN[posIN] - 128) << 6);
			posIN++;
			temp = temp + (strIN[posIN] - 128);
		} else if ((strIN[posIN] >= 192) && (strIN[posIN] < 224)) {
			temp = (strIN[posIN] - 192) << 6;
			posIN++;
			temp = temp + (strIN[posIN] - 128);
		} else {
			temp = strIN[posIN];
		}
		posIN++;

		if (temp < 128) {
			strOUT[posOUT] = temp;
			posOUT++;
		} else if (isA(temp) == 1) {
			strOUT[posOUT] = 'A';
			posOUT++;
		} else if (isE(temp) == 1) {
			strOUT[posOUT] = 'E';
			posOUT++;
		} else if (isI(temp) == 1) {
			strOUT[posOUT] = 'I';
			posOUT++;
		} else if (isO(temp) == 1) {
			strOUT[posOUT] = 'O';
			posOUT++;
		} else if (isU(temp) == 1) {
			strOUT[posOUT] = 'U';
			posOUT++;
		} else if (isY(temp) == 1) {
			strOUT[posOUT] = 'Y';
			posOUT++;
		} else if (isD(temp) == 1) {
			strOUT[posOUT] = 'D';
			posOUT++;
		} else if (isA(temp) == 2) {
			strOUT[posOUT] = 'a';
			posOUT++;
		} else if (isE(temp) == 2) {
			strOUT[posOUT] = 'e';
			posOUT++;
		} else if (isI(temp) == 2) {
			strOUT[posOUT] = 'i';
			posOUT++;
		} else if (isO(temp) == 2) {
			strOUT[posOUT] = 'o';
			posOUT++;
		} else if (isU(temp) == 2) {
			strOUT[posOUT] = 'u';
			posOUT++;
		} else if (isY(temp) == 2) {
			strOUT[posOUT] = 'y';
			posOUT++;
		} else if (isD(temp) == 2) {
			strOUT[posOUT] = 'd';
			posOUT++;
		} else {
			strOUT[posOUT] = '?';
			posOUT++;
		}
	}
	strOUT[posOUT] = '\0';
}
